package samfeng.erp.location.controller.admin;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import samfeng.erp.location.controller.vo.*;
import samfeng.erp.location.convert.LocationConvert;
import samfeng.erp.location.dal.dataobject.LocationDO;
import samfeng.erp.location.service.LocationService;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Api(tags = "管理后台 - 仓库库位")
@RestController
@RequestMapping("/stock/location")
@Validated
public class LocationController {

    @Resource
    private LocationService locationService;

    @PostMapping("/create")
    @ApiOperation("创建仓库库位")
    @PreAuthorize("@ss.hasPermission('stock:location:create')")
    public CommonResult<Long> createLocation(@Valid @RequestBody LocationCreateReqVO createReqVO) {
        return success(locationService.createLocation(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新仓库库位")
    @PreAuthorize("@ss.hasPermission('stock:location:update')")
    public CommonResult<Boolean> updateLocation(@Valid @RequestBody LocationUpdateReqVO updateReqVO) {
        locationService.updateLocation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除仓库库位")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('stock:location:delete')")
    public CommonResult<Boolean> deleteLocation(@RequestParam("id") Long id) {
        locationService.deleteLocation(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得仓库库位")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('stock:location:query')")
    public CommonResult<LocationRespVO> getLocation(@RequestParam("id") Long id) {
        LocationDO location = locationService.getLocation(id);
        return success(LocationConvert.INSTANCE.convert(location));
    }

    @GetMapping("/list")
    @ApiOperation("获得仓库库位列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('stock:location:query')")
    public CommonResult<List<LocationRespVO>> getLocationList(@RequestParam("ids") Collection<Long> ids) {
        List<LocationDO> list = locationService.getLocationList(ids);
        return success(LocationConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得仓库库位分页")
    @PreAuthorize("@ss.hasPermission('stock:location:query')")
    public CommonResult<PageResult<LocationRespVO>> getLocationPage(@Valid LocationPageReqVO pageVO) {
        PageResult<LocationDO> pageResult = locationService.getLocationPage(pageVO);
        return success(LocationConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出仓库库位 Excel")
    @PreAuthorize("@ss.hasPermission('stock:location:export')")
    @OperateLog(type = EXPORT)
    public void exportLocationExcel(@Valid LocationExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<LocationDO> list = locationService.getLocationList(exportReqVO);
        // 导出 Excel
        List<LocationExcelVO> datas = LocationConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "仓库库位.xls", "数据", LocationExcelVO.class, datas);
    }

    @PreAuthorize("@ss.hasPermission('stock:location:query')")
    @GetMapping("/list/exclude")
    public CommonResult<List<LocationRespVO>> excludeChild(@RequestParam("id") Long id, @RequestParam("warehouseId") Long warehouseId)
    {
        List<LocationRespVO> stockLocations = locationService.excludeChild(id, warehouseId);
        return success(stockLocations);
    }

}
